package com.wenhui.schoolyardinfo.firstpage.ui;

import android.graphics.Rect;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.nex3z.flowlayout.FlowLayout;
import com.qmuiteam.qmui.widget.QMUITopBar;
import com.wenhui.schoolyardinfo.R;
import com.wenhui.schoolyardinfo.base.BaseActivity;
import com.wenhui.schoolyardinfo.callback.FlowLayoutHotSearchOnItemViewClickListener;
import com.wenhui.schoolyardinfo.callback.FlowLayoutOnItemViewClickListener;
import com.wenhui.schoolyardinfo.customview.CircleImageView;
import com.wenhui.schoolyardinfo.firstpage.adapter.BaseRecyclerAdapter;
import com.wenhui.schoolyardinfo.firstpage.bean.LikePersonBean;
import com.wenhui.schoolyardinfo.firstpage.holder.SmartViewContentHolder;
import com.wenhui.schoolyardinfo.manager.ScreenManager;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * author    : Urasaki
 * e-mail    : 1164688204@qq.com
 * date      : 2019/6/15 17:22
 * introduce :
 */

public class SearchActivity extends BaseActivity {

    private static final String TAG = "SearchActivity";
    @BindView(R.id.imv_back)
    ImageView imvBack;
    @BindView(R.id.layout_back)
    RelativeLayout layoutBack;
    @BindView(R.id.topbar)
    QMUITopBar topbar;
    @BindView(R.id.edt_input)
    EditText edtInput;
    @BindView(R.id.tev_search)
    TextView tevSearch;
    @BindView(R.id.tev_historic_records)
    TextView tevHistoricRecords;
    @BindView(R.id.tev_clean_up)
    TextView tevCleanUp;
    @BindView(R.id.flow_historic_records)
    FlowLayout flowHistoricRecords;
    @BindView(R.id.layout_historic_records)
    LinearLayout layoutHistoricRecords;
    @BindView(R.id.tev_hot_search)
    TextView tevHotSearch;
    @BindView(R.id.tev_exchange)
    TextView tevExchange;
    @BindView(R.id.flow_hot_search)
    FlowLayout flowHotSearch;
    @BindView(R.id.tev_guess_you_like)
    TextView tevGuessYouLike;
    @BindView(R.id.guess_you_like_recycler_view)
    RecyclerView guessYouLikeRecyclerView;

    private List<String> historicRecordsList;
    private List<String> hotSearchList;
    private List<TextView> historicRecordsTextViewList;
    private List<TextView> hotSearchTextViewList;
    private GridLayoutManager layoutManager;
    private BaseRecyclerAdapter<LikePersonBean> baseRecyclerAdapter;
    private List<LikePersonBean> likePersonBeanList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);
        ButterKnife.bind(this);

        initData();
        initViews();
    }

    @Override
    protected void initData() {
        historicRecordsList = new ArrayList<>();
        historicRecordsList.add("采风照");
        historicRecordsList.add("采风照");
        historicRecordsList.add("采风照");
        historicRecordsList.add("采风照");
        historicRecordsList.add("采风照");
        historicRecordsList.add("采风照");
        historicRecordsList.add("爱吃鱼的猫");
        historicRecordsList.add("情感语录");
        historicRecordsList.add("情感语录");
        historicRecordsList.add("采风照");
        historicRecordsList.add("爱吃鱼的猫");
        historicRecordsList.add("爱吃鱼的猫");
        hotSearchList = new ArrayList<>();
        hotSearchList.add("采风照");
        hotSearchList.add("采风照");
        hotSearchList.add("采风照");
        hotSearchList.add("采风照");
        hotSearchList.add("采风照");
        hotSearchList.add("采风照");
        hotSearchList.add("爱吃鱼的猫");
        hotSearchList.add("情感语录");
        hotSearchList.add("情感语录");
        hotSearchList.add("采风照");
        hotSearchList.add("爱吃鱼的猫");
        hotSearchList.add("爱吃鱼的猫");

        historicRecordsTextViewList = new ArrayList<>();
        hotSearchTextViewList = new ArrayList<>();

        likePersonBeanList = new ArrayList<>();
        likePersonBeanList.add(new LikePersonBean("http://ww1.sinaimg.cn/large/0065oQSqly1fsfq1k9cb5j30sg0y7q61.jpg", "隆美尔元帅"));
        likePersonBeanList.add(new LikePersonBean("http://ww1.sinaimg.cn/large/0065oQSqly1fsfq1k9cb5j30sg0y7q61.jpg", "隆美尔元帅"));
        likePersonBeanList.add(new LikePersonBean("http://ww1.sinaimg.cn/large/0065oQSqly1fsfq1k9cb5j30sg0y7q61.jpg", "隆美尔元帅"));
        likePersonBeanList.add(new LikePersonBean("http://ww1.sinaimg.cn/large/0065oQSqly1fsfq1k9cb5j30sg0y7q61.jpg", "隆美尔元帅"));
        likePersonBeanList.add(new LikePersonBean("http://ww1.sinaimg.cn/large/0065oQSqly1fsfq1k9cb5j30sg0y7q61.jpg", "隆美尔元帅"));
    }

    @Override
    protected void initViews() {
        setTopBar(topbar);
        topbar.removeAllLeftViews();
        edtInput.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {

            //当键盘弹出隐藏的时候会 调用此方法。
            @Override
            public void onGlobalLayout() {
                final Rect rect = new Rect();
                getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);
                final int screenHeight = getWindow().getDecorView().getRootView().getHeight();
                final int heightDifference = screenHeight - rect.bottom;
                boolean visible = heightDifference > screenHeight / 3;
                if (visible) {
                    Log.i(TAG, "软键盘显示");

                } else {
                    Log.i(TAG, "软键盘隐藏");
                    edtInput.clearFocus();
                }
            }
        });


        for (int i = 0; i < historicRecordsList.size(); i++) {
            TextView textView = buildHistoricRecordsLabel(historicRecordsList.get(i), i);
            flowHistoricRecords.addView(textView);
            historicRecordsTextViewList.add(textView);
        }

        for (int i = 0; i < hotSearchList.size(); i++) {
            TextView textView = buildHotSearchLabel(hotSearchList.get(i), i);
            flowHotSearch.addView(textView);
            hotSearchTextViewList.add(textView);
        }

        initAdapter();
    }

    private void initAdapter() {
        layoutManager = new GridLayoutManager(this, 5);
        //不要加这一行
//        layoutManager.setOrientation(OrientationHelper.VERTICAL);

        guessYouLikeRecyclerView.setItemAnimator(new DefaultItemAnimator());
        guessYouLikeRecyclerView.setLayoutManager(layoutManager);
        baseRecyclerAdapter = new BaseRecyclerAdapter<LikePersonBean>(R.layout.item_like_person, 1) {
            @Override
            protected void onBindViewHolder(RecyclerView.ViewHolder holder, LikePersonBean model, int position) {
                if (holder instanceof SmartViewContentHolder) {
                    SmartViewContentHolder smartViewContentHolder = (SmartViewContentHolder) holder;
                    Glide.with(SearchActivity.this)
                            .load(model.getHeadPortraitUrl())
                            .into((CircleImageView) smartViewContentHolder.getView(R.id.imv_head_portrait));
                    smartViewContentHolder.text(R.id.tev_name, model.getName());
                }
            }
        };
        guessYouLikeRecyclerView.setAdapter(baseRecyclerAdapter);

        baseRecyclerAdapter.clearData();
        baseRecyclerAdapter.addAllData(likePersonBeanList);
    }

    private TextView buildHistoricRecordsLabel(String text, final int position) {
        final TextView textView = new TextView(this);
        setFlowLayoutOnItemViewClickListener(new FlowLayoutOnItemViewClickListener() {
            @Override
            public void onItemFlowLayoutClickListener(int position, View view) {
                for (int i = 0; i < historicRecordsList.size(); i++) {
                    if (i == position) {
                        historicRecordsTextViewList.get(i).setBackgroundResource(R.drawable.bg_corners_2_blue_a_stroke_1_blue_b);
                        showToast("点击了子项");
                    } else {
                        historicRecordsTextViewList.get(i).setBackgroundResource(R.drawable.bg_corners_2_gray_c);
                    }
                }

                for (int i = 0; i < hotSearchList.size(); i++) {
                        hotSearchTextViewList.get(i).setBackgroundResource(R.drawable.bg_corners_2_gray_c);
                }
            }
        });
        textView.setText(text);
        textView.setTextColor(getResources().getColor(R.color.colorBlackA));
        textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12);
        textView.setPadding(ScreenManager.dipTopx(this, 9),
                ScreenManager.dipTopx(this, 7),
                ScreenManager.dipTopx(this, 9),
                ScreenManager.dipTopx(this, 7));
        textView.setBackgroundResource(R.drawable.bg_corners_2_gray_c);
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                flowLayoutOnItemViewClickListener.onItemFlowLayoutClickListener(position, view);
            }
        });

        return textView;
    }

    private TextView buildHotSearchLabel(String text, final int position) {
        final TextView textView = new TextView(this);
        setFlowLayoutHotSearchOnItemViewClickListener(new FlowLayoutHotSearchOnItemViewClickListener() {
            @Override
            public void onItemFlowLayoutHotSearchClickListener(int position, View view) {
                for (int i = 0; i < historicRecordsList.size(); i++) {
                        historicRecordsTextViewList.get(i).setBackgroundResource(R.drawable.bg_corners_2_gray_c);
                }

                for (int i = 0; i < hotSearchList.size(); i++) {
                    if (i == position) {
                        hotSearchTextViewList.get(i).setBackgroundResource(R.drawable.bg_corners_2_blue_a_stroke_1_blue_b);
                        showToast("点击了子项");
                    } else {
                        hotSearchTextViewList.get(i).setBackgroundResource(R.drawable.bg_corners_2_gray_c);
                    }
                }
            }
        });
        textView.setText(text);
        textView.setTextColor(getResources().getColor(R.color.colorBlackA));
        textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12);
        textView.setPadding(ScreenManager.dipTopx(this, 9),
                ScreenManager.dipTopx(this, 7),
                ScreenManager.dipTopx(this, 9),
                ScreenManager.dipTopx(this, 7));
        textView.setBackgroundResource(R.drawable.bg_corners_2_gray_c);
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                flowLayoutHotSearchOnItemViewClickListener.onItemFlowLayoutHotSearchClickListener(position, view);
            }
        });

        return textView;
    }

    private FlowLayoutOnItemViewClickListener flowLayoutOnItemViewClickListener;

    public void setFlowLayoutOnItemViewClickListener(FlowLayoutOnItemViewClickListener flowLayoutOnItemViewClickListener) {
        this.flowLayoutOnItemViewClickListener = flowLayoutOnItemViewClickListener;
    }

    private FlowLayoutHotSearchOnItemViewClickListener flowLayoutHotSearchOnItemViewClickListener;

    public void setFlowLayoutHotSearchOnItemViewClickListener(FlowLayoutHotSearchOnItemViewClickListener flowLayoutHotSearchOnItemViewClickListener) {
        this.flowLayoutHotSearchOnItemViewClickListener = flowLayoutHotSearchOnItemViewClickListener;
    }

    @OnClick({R.id.layout_back, R.id.tev_search, R.id.tev_clean_up, R.id.tev_hot_search, R.id.tev_exchange})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.layout_back:
                finish();
                break;
            case R.id.tev_search:
                startActivity(SearchTermActivity.class);
                break;
            case R.id.tev_clean_up:
                layoutHistoricRecords.setVisibility(View.GONE);
                break;
            case R.id.tev_hot_search:
                break;
            case R.id.tev_exchange:
                break;
        }
    }
}
